<?php

namespace app\admin\command;

use app\admin\model\exchange\Order;
use think\addons\AddonException;
use think\addons\Service;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\exception\PDOException;

class Storeorderreport extends Command
{
    protected function configure()
    {
        $this
            ->setName('sor')
            ->setDescription('店铺订单每日统计');
    }

    protected function execute(Input $input, Output $output)
    {
        //根据店铺获取前一天订单数据
        $yestodaytime_tmp = strtotime('-1 day');
        $yestodaydate = date('Y-m-d',$yestodaytime_tmp);
        $yestodaytime = strtotime(date('Y-m-d 00:00:00',$yestodaytime_tmp));
        $now_date  = date('Y-m-d 00:00:00');
        $now_date_time = strtotime($now_date);

        $all_order_tmp = Order::where('createtime','>=',$yestodaytime)->where('createtime','<',$now_date_time)->where('is_pay',2)->select();
//        var_dump($all_order_tmp);die;
        if($all_order_tmp){
            $all_order = [];
            $all_save_info = [];
            foreach ($all_order_tmp as  $item){
                if(isset($item->store_no) && $item->store_no){
                    $tmp_store_no = $item->store_no.'##@##'.$item->agent_id;
                    $all_order[$tmp_store_no]['pay_price'][] = $item->total_pay_price;
                    $all_order[$tmp_store_no]['pay_num'][] = 1;
                    if($item->is_pay == 1){
                        //退款
                        $all_order[$tmp_store_no]['refund_price'][] = $item->total_pay_price;
                        $all_order[$tmp_store_no]['refund_num'][] = 1;
                    }

                    if($item->is_pay == 2){
                        //已付款
                        $all_order[$tmp_store_no]['real_price'][] = $item->total_pay_price;
                        $all_order[$tmp_store_no]['real_num'][] = 1;
                    }

                    unset($tmp_store_no);
                }
            }
//            var_dump($all_order);die;
            if ($all_order){
                foreach ($all_order as $key=>$val){
                    $tmp['store_no'] = 0;
                    $tmp['agent_id'] = 0;
                    $store_no_agent_id_arr = explode('##@##',$key);
                    if(isset($store_no_agent_id_arr[0]) && $store_no_agent_id_arr[0]){
                        $tmp['store_no'] = $store_no_agent_id_arr[0];
                    }
                    if(isset($store_no_agent_id_arr[1]) && $store_no_agent_id_arr[1]){
                        $tmp['agent_id'] = $store_no_agent_id_arr[1];
                    }
                    if(!$tmp['store_no'] || !$tmp['agent_id']){
                        continue;
                    }
                    $tmp['pay_price'] = isset($val['pay_price']) && $val['pay_price'] ? array_sum($val['pay_price']):0;
                    $tmp['pay_num'] = isset($val['pay_num']) && $val['pay_num'] ? count($val['pay_num']):0;
                    $tmp['refund_price'] = isset($val['refund_price']) && $val['refund_price'] ? array_sum($val['refund_price']):0;
                    $tmp['refund_num'] = isset($val['refund_num']) && $val['refund_num'] ? count($val['refund_num']):0;
                    $tmp['real_price'] = isset($val['real_price']) && $val['real_price'] ? array_sum($val['real_price']):0;
                    $tmp['real_num'] = isset($val['real_num']) && $val['real_num'] ? count($val['real_num']):0;
                    $tmp['date'] = $yestodaydate;

                    $all_save_info[] = $tmp;

                    unset($tmp);
                }
            }

            $order_report_model = new \app\admin\model\report\Order();
            //删除原来数据
            $order_report_model->where('date',$yestodaydate)->delete();

            //新增数据
            if($all_save_info){
                $order_report_model->saveAll($all_save_info);
            }

        }

    }



}

//cd /data0/web/store.leyinshuke.com  && php think sor >> /data0/wwwlogs/1Storeorderreport.log 2>&1
